package com.shanghaionstar.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.onstar.cn.sqlite.entities.RequestEventReportEntity;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: OperaterReportDBService
 * @Description: 事件上传report
 * @author Xiaofeng Feng
 * @date 2015年8月20日 下午2:15:18
 */
public class OperaterReportDBService extends BaseSqlImp {

	public OperaterReportDBService(Context context) {
		super(context);
	}

	/**
	 * 判断某表里某字段是否存在
	 *
	 * @param db
	 * @param tableName
	 * @param fieldName
	 * @return
	 */
	public boolean isFieldExist(SQLiteDatabase db, String tableName, String fieldName) {
		String queryStr = "select sql from sqlite_master where type = 'table' and name = '%s'";
		queryStr = String.format(queryStr, tableName);
		Cursor c = db.rawQuery(queryStr, null);
		String tableCreateSql = null;
		try {
			if (c != null && c.moveToFirst()) {
				tableCreateSql = c.getString(c.getColumnIndex("sql"));
			}
		} finally {
			if (c != null)
				c.close();
		}
		return tableCreateSql != null && tableCreateSql.contains(fieldName);
	}

	/**
	 * @Title: insert
	 * @Description: insert data to table
	 * @param @table report 
	 * @param @param entity 
	 * @param @return 设定文件 
	 * @returnlong 返回类型 @throws
	 */
	public long insert(RequestEventReportEntity entity) {
		long rowId = 0;
		try {
			mDbUtils.save(entity);
			rowId = mDbUtils.count(RequestEventReportEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return rowId;
	}

	/**
	 * @Title: insert @Description: insert data to table @param @table
	 * report @param @param list @param @return 设定文件 @return long
	 * 返回类型 @throws
	 */
	public long insert(List<RequestEventReportEntity> list) {
		long rowId = 0;
		try {
//			for (RequestEventReportEntity entity : list) {
//				rowId = super.insert(entity);
//				if (rowId == -1) {
//					break;
//				}
//			}
			mDbUtils.saveAll(list);
			rowId = mDbUtils.count(RequestEventReportEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rowId;
	}
	
	/**
	 * @Title: delete @Description: delete data to table @param @table
	 * report @param @return 设定文件 @return void 返回类型 @throws
	 */
	public void delete(String id) {
		// TODO Auto-generated method stub
		try {
			mDbUtils.delete(RequestEventReportEntity.class,WhereBuilder.b("_id","=",Integer.parseInt(id)));
		} catch (DbException e) {
			e.printStackTrace();
		}
//		super.delete("_id=? ", new String[] { id });
	}
	
	/**
	 * @Title: deleteAll @Description: deleteAll data to table @param @table
	 * report @param @return 设定文件 @return void 返回类型 @throws
	 */
	public void deleteAll(List<RequestEventReportEntity> eventList, boolean isGetAll) {
		try {
			String orderBy = null;
//			if (!isGetAll) {
//				orderBy = RequestEventReportEntity.TIME_STAMP + " desc ";
//			}
			
			if (eventList == null) {
//				eventList = getAll(null, null, null, null, null, orderBy, "0,50");
				eventList = mDbUtils.findAll(Selector.from(RequestEventReportEntity.class).limit(50));
			}
//
//			for (RequestEventReportEntity entity : eventList) {
//				if (entity.getId() != null)
//					delete(entity.getId().toString());
//			}
			mDbUtils.deleteAll(eventList);
		} catch (Exception e) {
			e.printStackTrace();
			eventList = new ArrayList<RequestEventReportEntity>();
		}
	}

	/**
	 * @Title: getList @Description: getList data to table @param @table
	 * report @param @return 设定文件 @return list 返回类型 @throws
	 */

	public List<RequestEventReportEntity> getList(boolean isGetAll) {
		List<RequestEventReportEntity> list = new ArrayList<RequestEventReportEntity>();
		try {
			String orderBy = null;
//			if (!isGetAll) {
//				orderBy = RequestEventReportEntity.TIME_STAMP + " desc ";
//			}
//			list = getAll(null, null, null, null, null, orderBy, "0,50");
			list = mDbUtils.findAll(Selector.from(RequestEventReportEntity.class).limit(50));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

//	/**
//	 * @Title: getCount @Description: getCount data to table @param @table
//	 * report @return Integer 返回类型 @throws
//	 */
//	public Integer getCount() {
//		List<RequestEventReportEntity> list = new ArrayList<RequestEventReportEntity>();
//		try {
//			String sql = "select * from " + RequestEventReportEntity.TABLE_NAME;
//			list= super.rawQuery(sql, null);
//			return list.size();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return 0;
//	}
	/**
	 * @Title: getCount @Description: getCount data to table @param @table
	 * report @return Integer 返回类型 @throws
	 */
	public Integer getCount() {
		try {
//			String sql = "select count(*) from " + RequestEventReportEntity.TABLE_NAME;
//			Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql, null);
//			int count = - 1;
//			if(cursor.moveToFirst()){
//				count = cursor.getInt(0);
//			}
//			cursor.close();
//			return count;
			return (int)mDbUtils.count(RequestEventReportEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
